package odl;

import gui.Gui;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Vector;

import parser.ParseException;
import parser.TokenMgrError;

public class Controller {

	private static Controller controller;
	public static Controller getController()
	{
		if(controller==null)
		{
			controller = new Controller();
		}
		return controller;
	}
	private Schema schema;
	private Attribute current_Attribute;
	private Interface current_Interface;
	private Operation current_Operation;
	private Parameter current_Parameter;


	private Relation current_Relation;

	public Controller ()
	{
		schema = null;
		setCurrent_Attribute(null);
		setCurrent_Interface(null);
		setCurrent_Operation(null);
		setCurrent_Parameter(null);
		setCurrent_Relation(null);

	}
	
	public void add_Schema(Schema _schema)
	{
		schema = _schema;
	}
	
	public void addAttribute(Attribute at){
		current_Interface.addAttribute(at);
	}
	
	public void addInterface(Interface i)
	{
		schema.getInterfaces().add(i);
		setCurrent_Interface(i);
	}
	
	public void addInterfaceInheritence(String s)
	{
		if (current_Interface != null)
		{
			current_Interface.addInheritence(s);
		}
	}

	public void addInterfaceKeyUnreferenced(String s)
	{
		if (current_Interface != null)
		{
			current_Interface.addKey_unreferenced(s);
		}
	}

	public void addOperation(Operation op){
		current_Interface.addOperation(op);
	}

	public void addOperationParameter(Parameter p){
		current_Operation.addParameter(p);
	}

	public void addRelation(Relation _re){
		this.current_Interface.addRelation(_re);
	}

	public void addRelationInverseInterface(String _s){
		current_Relation.addInverseInterface(_s);
	}

	public void addRelationInverseRelation(String _s){
		current_Relation.addInverseRelation(_s);
	}

	public void addRelationName(String _name){
		current_Relation.addName(_name);
	}

	public void addRelationTargetName(String _name){
		current_Relation.addTargetName(_name);
	}

	public void addRelationTargetType(String _type){
		current_Relation.addTargetType(_type);
	}

	public Schema get_Schema()
	{
		return schema;
	}

	public Attribute getCurrent_Attribute() {
		return current_Attribute;
	}

	public Interface getCurrent_Interface() {
		return current_Interface;
	}

	public Operation getCurrent_Operation() {
		return current_Operation;
	}

	public Parameter getCurrent_Parameter() {
		return current_Parameter;
	}

	public Relation getCurrent_Relation() {
		return current_Relation;
	}

	public Vector<String> getDetails(OdlElement e){
		return e.getDetails();
	}

	public Vector<String> getInterfaceAttributeNames(String i_name){
		setCurrentInterface(i_name);
		return current_Interface.getAttributesNames();
	}

	public Vector<String> getInterfaceOperationNames(String i_name){
		setCurrentInterface(i_name);
		return current_Interface.getOperationsNames();
	}

	public Vector<String> getInterfaceRelationNames(String i_name){
		setCurrentInterface(i_name);
		return current_Interface.getRelationsNames();
	}

	public Vector<String> getOperationParameterNames(String o_name){
		setCurrentOperation(o_name);
		return current_Operation.getParametersNames();
	}

	public Vector<String> getSchemaInterfaceNames(){
		
		return schema.getInterfacesNames();
	}

	public void setCurrent_Attribute(Attribute current_Attribute) {
		this.current_Attribute = current_Attribute;
	}

	public void setCurrent_Interface(Interface current_Interface) {
		this.current_Interface = current_Interface;
	}

	public void setCurrent_Operation(Operation current_Operation) {
		this.current_Operation = current_Operation;
	}

	public void setCurrent_Parameter(Parameter current_Parameter) {
		this.current_Parameter = current_Parameter;
	}

	public void setCurrent_Relation(Relation current_Relation) {
		this.current_Relation = current_Relation;
	}
	
	public void setCurrentAttribute(String name){
		Attribute a = current_Interface.getAttribute(name);
		if (a != null) current_Attribute = a;
	}
	
	public void setCurrentInterface(String name){
		Interface i = schema.getInterface(name);
		if (i != null) current_Interface = i;
	}
	
	public void setCurrentOperation(String name){
		Operation op = current_Interface.getOperation(name);
		if (op != null) current_Operation = op;
	}
	
	public void setCurrentParameter(String name){
		Parameter pa = current_Operation.getParameter(name);
		if (pa != null) current_Parameter = pa;
	}
	
	public void setCurrentRelation(String name){
		Relation re = current_Interface.getRelation(name);
		if (re != null) current_Relation = re;
	}
	
	public void updateContent(){
		schema.updateContent();
		schema.calculateMetrics();
	}

	public void open(File file) throws FileNotFoundException, ParseException, TokenMgrError {
		parser.Odl.setInput(file);
		parser.Odl.run();
		Gui.getGUI().updateInterfaces(controller
				.getSchemaInterfaceNames());
		
	}
}
